import 'dart:io';

import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';

class Gallery extends StatefulWidget {
  Gallery({Key? key, required this.galleryData,required this.changePickPicState,required this.isPureReview,required this.setIndex}) : super(key: key);
  final List galleryData;
  final changePickPicState;
  final isPureReview;
  final setIndex;
  @override
  State<StatefulWidget> createState() => _Gallery();
}

class _Gallery extends State<Gallery> {
  int _current = 0;
  final CarouselSliderController _controller = CarouselSliderController();
  List selectPicsArr = []; //当前选中的图片

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      Container(
        child: CarouselSlider.builder(
          itemCount: widget.galleryData.length,
          itemBuilder:(context, index, realIdx){
            final item = widget.galleryData[index];
            return InkWell(
              onTap: (){
                setState(() {
                  final itemPath = item['tgt'];
                  var copyArr = selectPicsArr;
                  final checkContain = selectPicsArr.any((item)=>item['tgt']==itemPath);
                  if(checkContain){
                    copyArr.removeWhere((item)=>item['tgt']==itemPath);
                  }else{
                    copyArr.add(item);
                  }
                  selectPicsArr = copyArr;
                  widget.changePickPicState(copyArr);
                });

              },
              child: Container(
                margin: EdgeInsets.all(5.0),
                child: ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(5.0)),
                  child: Stack(
                    children: <Widget>[
                      Image.file(
                        File(item['tgt']),
                        fit: BoxFit.cover,
                        width: item['width'],
                        height: item['height'],
                      ),
                      Visibility(
                        visible:((!widget.isPureReview) && selectPicsArr.any((selectItem)=>selectItem['tgt']==item['tgt'])),
                          child: Positioned(
                          top:15.0,
                          right:15.0,
                          child: Container(
                              decoration: BoxDecoration(shape: BoxShape.circle),
                              width: 22,
                              height: 22,
                              child: SvgPicture.asset('assets/images/translate_photo_is_select.svg')
                              ))
                      ),
                      Visibility(
                          visible:((!widget.isPureReview) && !(selectPicsArr.any((selectItem)=>selectItem['tgt']==item['tgt']))),
                          child: Positioned(
                          top:15.0,
                          right:15.0,
                          child: Container(
                              decoration: BoxDecoration(shape: BoxShape.circle),
                              width: 22,
                              height: 22,
                              child: SvgPicture.asset('assets/images/translate_photo_not_select.svg'))
                      )),
                ]
                  ),
                ),
              ));
          },
          // items: imageSliders,
          carouselController: _controller,
          options: CarouselOptions(
              enlargeCenterPage: true,
              aspectRatio: 2.0,
              height: widget.galleryData[_current]['height'],
              onPageChanged: (index, reason) {
                setState(() {
                  _current = index;
                });
               widget.setIndex(index);
              }),
        ),
      ),
      SizedBox(height: 20,),
      Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: widget.galleryData.asMap().entries.map((entry) {
          return GestureDetector(
            onTap: () => _controller.animateToPage(entry.key),
            child: Container(
              width: 8.0,
              height: 8.0,
              margin: EdgeInsets.symmetric(vertical: 8.0, horizontal: 4.0),
              decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: (Theme.of(context).brightness == Brightness.dark
                      ? Colors.white
                      : Colors.black)
                      .withOpacity(_current == entry.key ? 0.9 : 0.4)),
            ),
          );
        }).toList(),
      ),
    ]);
  }
}
